#include "blockgroupmove.h"
#include "block.h"

BlockGroupMove::BlockGroupMove(const QPointF &point, QObject *parent)
    : BlockGroup(point, parent)
{
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, [this]{
//        qDebug() << "move one step";
        move_one_step(down);
        if(isCollding()){
            move_one_step(up);
            emit need_next_shape();
        }
    });
    connect(this, &BlockGroupMove::game_over, [this]{
        timer->stop();
        setFlags(flags() ^ QGraphicsItem::ItemIsFocusable);       //使得方块组支持键盘操作
    });
    connect(this, &BlockGroupMove::need_next_shape, [this]{
//        qDebug()<<"next_shape"<<"AAA";
        moveBlockFromGroup();
//        this->newShape();
    });
}

/*
 * @Brief:  游戏开始
 * @Detail: 方块开始下落
 */
void BlockGroupMove::game_start()
{
    setFlags(QGraphicsItem::ItemIsFocusable);       //使得方块组支持键盘操作
    this->setFocus();
    //读取设置的初始游戏速度
    setSpeed(ST->settings()->value("GameSpeed", 0).toInt());
    timer->start();
    this->newShape();           //只有开始时是新建，其余都是复制
}

int BlockGroupMove::speed() const
{
    return m_speed;
}

/*
 * @Brief:  设置游戏速度
 * @Detail: 同时改变定时器间隔，规则如下：
 *          0速 : 1000ms
 *          1速 : 900ms
 *          2速 : 800ms
 *          3速 : 700ms
 *          4速 : 600ms
 *          5速 : 500ms
 *          6速 : 400ms
 *          7速 : 300ms
 *          8速 : 200ms
 *          9速 : 100ms
 */
void BlockGroupMove::setSpeed(int newSpeed)
{
    m_speed = newSpeed;
    timer->setInterval(100 * (10 - m_speed));
}

/*
 * @Brief:  拷贝另一个方块组
 */
void BlockGroupMove::copyShape(BlockGroup *another)
{
    m_blockshape = another->blockshape();
    m_rotation = another->rotation();

    deleteBlockFromGroup();
    blockListRemake();
    setRotation(m_rotation);

    setPos(startPos);
    if(isCollding()){
        qDebug()<<"游戏结束";
//        deleteBlockFromGroup();
        do {
            move_one_step(up);
        } while (isCollding());
        emit game_over();
    }
}

/*
 * @Brief:  键盘操控事件
 */
void BlockGroupMove::keyPressEvent(QKeyEvent *event)
{
//    qDebug()<<"11111"<<(Qt::Key)event->key();
    switch (event->key()) {
    case Qt::Key_Left:
    case Qt::Key_A:
        move_one_step(left);
        if(isCollding()){
            move_one_step(right);
        }
        break;
    case Qt::Key_Right:
    case Qt::Key_D:
        move_one_step(right);
        if(isCollding()){
            move_one_step(left);
        }
        break;
    case Qt::Key_Up:        //旋转
    case Qt::Key_W:
        m_rotation += 90;
        setRotation(m_rotation);
        if(isCollding()) {
            qDebug()<<"isCollding";
            m_rotation -= 90;
            setRotation(m_rotation);
        }
        break;
    case Qt::Key_Down:      //下落
    case Qt::Key_S:
        move_one_step(down);
        if(isCollding()){
            move_one_step(up);
            emit need_next_shape();
        }        
        break;
    case Qt::Key_Space:     //快速下落
        move_one_step(down);
        while (!isCollding()) {
            move_one_step(down);
        }
        move_one_step(up);
        emit need_next_shape();
        break;
    default:
        break;
    }
}

/*
 * @Brief:  砖块移动一步
 * @Detail: 只有左、右、下三个方向
 */
void BlockGroupMove::move_one_step(Direction direction)
{
    int d = ST->blockSideLength();
    switch (direction) {
    case up:
        moveBy(0, -d);
        break;
    case down:
        moveBy(0, d);
        break;
    case left:
        moveBy(-d, 0);
        break;
    case right:
        moveBy(d, 0);
        break;
    default:
        break;
    }
}
